/**
 * +----------------------------------------------------------------------
 * | 「e家宜业」
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020-2024 https://www.chowa.cn All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed 未经授权禁止移除「e家宜业」和「卓佤科技」相关版权
 * +----------------------------------------------------------------------
 * | Author: contact@chowa.cn
 * +----------------------------------------------------------------------
 */

import nodemailer from 'nodemailer';
import moment from 'moment';
import config from '~/config';
import cwlog from 'chowa-log';

export interface MailOptions extends nodemailer.SendMailOptions {
    content: string[];
}

export async function send({ subject, content, attachments = [] }: MailOptions) {
    return new Promise(resolve => {
        const { host, port, secure, user, password, to } = config.smtp;

        if (!user || !password || !to) {
            return;
        }

        const html = `<!DOCTYPE HTML>
        <html>

        <head>
            <meta content="width=device-width" name="viewport" />
            <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
            <title>${subject}</title>
            <style type="text/css">
                body {
                    background-color: #f6f6f6;
                    font-family: sans-serif;
                    -webkit-font-smoothing: antialiased;
                    font-size: 14px;
                    line-height: 1.4;
                    margin: 0;
                    padding: 0;
                    overflow: hidden;
                }

                .mail-box {
                    max-width: 620px;
                    margin: auto;
                    padding: 22px 12px;
                }

                .mail-content {
                    background: #fff;
                    border-radius: 3px;
                    padding: 18px 16px 0 16px;
                }

                .to-user {
                    color: #454545;
                    margin-bottom: 10px
                }

                p {
                    padding: 0 26px;
                    margin-bottom: 6px;
                    color: #676767;
                    word-break: break-all
                }

                .send-time {
                    color: #898989;
                    text-align: right
                }

                .refer {
                    border-top: 1px solid#f5f5f5;
                    padding: 8px 0;
                    font-size: 12px;
                    color: #999
                }

                .logo {
                    width: 22px;
                    height: 22px;
                    float: right;
                }

                .copy {
                    text-align: center;
                    font-size: 12px;
                    padding: 12px 0;
                }

                .copy a {
                    color: #999;
                    text-decoration: none;
                    transition: all 0.2s;
                }

                .copy a:hover {
                    color: #2d8cf0;
                }

            </style>
        </head>

        <body>
            <div class="mail-box">
                <div class="mail-content">
                    <div class="to-user">
                        运维报警，
                    </div>
                    ${content.map(str => `<p>${str}</p>`).join('')}
                    <p class="send-time">
                        ${moment().format('YYYY-MM-DD HH:mm:ss')}
                    </p>
                    <div class="refer">
                        此邮件由系统自动发送，请勿回复

                        <img class="logo" src=''/>
                    </div>
                </div>

                <div class="copy"><a href="https://www.chowa.cn">&copy;卓佤科技 2020-${moment().year()}</a></div>
            </div>
        </body>

        </html>`;

        nodemailer
            .createTransport({
                host,
                port,
                secure,
                auth: {
                    user,
                    pass: password
                }
            })
            .sendMail(
                {
                    from: `e家宜业服务报警 <${user}>`,
                    to: to,
                    subject: `e家宜业服务报警「${subject}」`,
                    html,
                    attachments
                },
                err => {
                    if (err) {
                        cwlog.error('邮件发送失败！');
                        console.log(err);
                        resolve(false);
                    }

                    resolve(true);
                }
            );
    });
}
